package com.main.hotel.repository;

import com.main.hotel.entity.Room;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author 莫浚洋
 */

@Repository
public interface RoomRepository extends JpaRepository<Room, Integer>,
  JpaSpecificationExecutor<Room> {

  @Query(value = "select count(1) from room where room_type= ?1", nativeQuery = true)
  int findRoomCount(String room_type);

  @Modifying
  @Transactional
  @Query(value = "update room set room_status=2,order_id=?2 where room_type= ?1 and room_status=1 limit 1", nativeQuery = true)
  int book(String room_type,String order_id);

  @Query(value = "select room_id from room where order_id= ?1", nativeQuery = true)
  int findRoomId(String order_id);

  @Query(value = "select room_number from room where order_id= ?1", nativeQuery = true)
  String findRoomNum(String order_id);
  @Modifying
  @Transactional
  @Query(value = "update room set room_status=1 where order_id= ?1", nativeQuery = true)
  int cancelBook(String order_id);

  @Query (value = "select count(*) from room where roomtype_id = ?1 and room_status = 1", nativeQuery = true)
  public int findRoomNumByRoomtype_id(Integer roomtype_id);
  @Query (value = "select * from room where room_number = ?1", nativeQuery = true)
  public Room findByRoom_number(String room_number);


}
